package com.aos.erp.schools.rest.coach;

import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.schools.enums.student.IsDel;
import com.aos.erp.schools.model.coach.CoachCar;
import com.aos.erp.schools.model.coach.Dto.CoachCarDto;
import com.aos.erp.schools.service.coach.CoachCarService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @program: erp
 * @description: 教练关联车辆
 * @author: tanzhiyu
 * @create: 2018-11-12 14:10
 **/
@Api(value = "教练关联车辆", tags = {"教练关联车辆API"})
@RestController("RESTCoachCarController")
@RequestMapping("/api/coachCar")
public class CoachCarController {
    @Resource
    private CoachCarService coachCarService;

    @ApiOperation(value = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coach_id", value = "教练ID", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "carId", value = "车辆ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/addCoachCar")
    public ResultEntity addCoachCar(CoachCar coachCar) throws Exception {
        coachCarService.addBean(coachCar);
        return ResultEntity.success();
    }

    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coachId", value = "教练ID", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "carId", value = "车辆ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/updatePosition")
    public ResultEntity updatePosition(CoachCar coachCar) throws Exception {
        coachCarService.updateObject(coachCar);
        return ResultEntity.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coachId", value = "教练ID", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "carId", value = "车辆ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/delCoachCar")
    public ResultEntity delCoachCar(String coachId,String carId) throws Exception {
        if (StringUtils.isBlank(coachId)|| StringUtils.isBlank(carId)) {
            return ResultEntity.failed("教练关联车辆ID不能为空", ResultEntity.CODE_NULL);
        }
        CoachCar coachCar=new CoachCar();
        coachCar.setIsDel(Integer.valueOf(IsDel.del.getValue().toString()));
        coachCar.setCarId(Long.valueOf(carId));
        coachCar.setCoachId(Long.valueOf(coachId));
        int i = coachCarService.updateObject(coachCar);
        if (i > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.success("删除失败");
        }
    }

    @ApiOperation(value = "根据教练关联车辆ID获取信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教练关联车辆ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getInfo")
    public ResultEntity getInfo(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("教练关联车辆ID不能为空", ResultEntity.CODE_NULL);
        }
        CoachCarDto coachCarDto = coachCarService.getInfo(id);
        return ResultEntity.success(coachCarDto);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = " 每页显示条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "coachId", value = "教练id", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "coachName", value = "教练名称", required = false, dataType = "String", paramType = "Query")
    })
    public ResultEntity<IPage<CoachCarDto>> list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                          @RequestParam(name = "limit", defaultValue = "10") Integer limit,
                                                          @RequestParam(name = "carId", required = true) String carId,
                                                          @RequestParam(name = "coachId", required = false) String coachId,
                                                          @RequestParam(name = "coachName", required = false) String coachName,
                                                          String currentUserId)throws Exception {
        //TODO 部门组织code
        String depCode= UserUtil.getUserDepCode(currentUserId);
        return ResultEntity.success(coachCarService.selectByParams(page, limit, carId,coachId,coachName,depCode));
    }
}
